package com.kly.cashmall.module.products.presenter;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.kly.cashmall.bean.OrderConfirmBean;
import com.kly.cashmall.bean.ProductDetailsConfirmBean;
import com.kly.cashmall.bean.ProtocolBean;
import com.kly.cashmall.framework.mvp.BaseViewPresenter;
import com.kly.cashmall.services.Network;
import com.kly.cashmall.services.Params;
import com.kly.cashmall.utils.function.ToastUtil;
import com.kly.cashmall.widget.Loading.LoadingDialog;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * @author
 */
public class ProductDetailConfirmPresenter extends BaseViewPresenter<ProductDetailConfirmViewer> {

    public ProductDetailConfirmPresenter(ProductDetailConfirmViewer viewer) {
        super(viewer);
    }

    @Override
    public void willDestroy() {

    }

    public void getOrderSubmitList(String productCode, String merchantCode) {
        LoadingDialog.showLoading(getActivity());
        Params params = new Params();
        params.put("productCode", productCode);
        params.put("merchantCode", merchantCode);
        rxManager.add(Network.getApi().getOrderSubmit(params.buildPostBody()).subscribeOn(Schedulers.io()).observeOn
                (AndroidSchedulers.mainThread()).subscribe(new Consumer<ProductDetailsConfirmBean>() {
            @Override
            public void accept(ProductDetailsConfirmBean bean) throws Exception {
                LoadingDialog.dismissLoading();
                if (getViewer() != null) {
                    getViewer().getOrderSubmitListSuccess(bean);
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                LoadingDialog.dismissLoading();
                ToastUtil.showToast(throwable.getMessage());
            }
        }));
    }

    public void getOrderSubmitPost(String productCode, String merchantCode, String usageOfLoan, JsonObject currentWifi, JsonArray nearbyWifis) {
        LoadingDialog.showLoading(getActivity());
        Params params = new Params();
        params.put("productCode", productCode);
        params.put("merchantCode", merchantCode);
        params.put("usageOfLoan", usageOfLoan);
        params.put("currentWifi", currentWifi);
        params.put("nearbyWifis", nearbyWifis);
        rxManager.add(Network.getApi().postOrderSubmit(params.buildPostBody()).subscribeOn(Schedulers.io()).observeOn
                (AndroidSchedulers.mainThread()).subscribe(new Consumer<OrderConfirmBean>() {
            @Override
            public void accept(OrderConfirmBean bean) throws Exception {
                LoadingDialog.dismissLoading();
                if (getViewer() != null) {
                    getViewer().getOrderSubmitPostSuccess(bean);
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                LoadingDialog.dismissLoading();
                ToastUtil.showToast(throwable.getMessage());
            }
        }));

    }

    public void getProtocol() {
        rxManager.add(Network.getApi().getProtocol().subscribeOn(Schedulers.io()).observeOn
                (AndroidSchedulers.mainThread()).subscribe(new Consumer<ProtocolBean>() {
            @Override
            public void accept(ProtocolBean bean) throws Exception {
                if (getViewer() != null) {
                    getViewer().getProtocolSuccess(bean);
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                //返回异常 如404 503，或者返回的数据类型和我们定义的实体对象不一致
            }
        }));
    }

}